<?php

namespace common\models\datas;

use common\components\bases\BaseModel;
use common\components\behaviors\SynchronousAccessBehavior;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use common\components\behaviors\UpdateCacheBehavior;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%menu}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $parent_id
 * @property string $m
 * @property string $c
 * @property string $a
 * @property string $param
 * @property integer $listorder
 * @property integer $display
 * @property string $size
 * @property string $module
 */
class Menu extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%menu}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'c', 'a'], 'required'],
            [['parent_id', 'listorder', 'display'], 'integer'],
            [['name'], 'string', 'max' => 60],
            [['icon'], 'string', 'max' => 255],
            [['m', 'c', 'a'], 'string', 'max' => 30],
            [['param'], 'string', 'max' => 100],
            [['position'], 'string', 'max' => 11],
            [['size'], 'string', 'max' => 20],
            [['size'], 'required', 'when' => function ($model) {
                return $model->display == 2 || $model->display == 4;
            }, 'whenClient' => "function (attribute, value) {
                return $('#menu-display input:checked').val() == '2'||$('#menu-display input:checked').val() == '4';
            }"],
            [['module'], 'string', 'max' => 10]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'name' => Yii::t('common', '菜单名称'),
            'parent_id' => Yii::t('common', '父菜单ID'),
            'm' => Yii::t('common', '模块'),
            'c' => Yii::t('common', '控制器'),
            'a' => Yii::t('common', '方法'),
            'param' => Yii::t('common', '参数'),
            'listorder' => Yii::t('common', '排序'),
            'display' => Yii::t('common', '显示方式'),
            'size' => Yii::t('common', '显示尺寸'),
            'module' => Yii::t('common', '所属模块'),
            'icon' => Yii::t('common', '图片'),
            'position' => Yii::t('common', '位置'),
            'message' => Yii::t('common', '提示')
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['display'] = function () {
            switch ($this->display) {
                case 0:
                    return \Yii::t('common', '不显示');
                case 1:
                    return \Yii::t('common', '显示');
                case 2:
                    return \Yii::t('common', '弹窗方式');
                case 3:
                    return \Yii::t('common', ' Action方式');
                case 4:
                    return \Yii::t('common', '记录项弹出方式');
                case 5:
                    return \Yii::t('common', '记录项Action方式');
                case 6:
                    return \Yii::t('common', '批量输入操作方式');
                case 7:
                    return \Yii::t('common', '批量选择操作方式');
            }
        };
        return $fields;
    }

    public function extraFields()
    {
        return [
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
            'frontend_ruleoperate' => function(){
                //return  Html::radio("openrule",false,['value'=>$this->id,'class'=>'myradio mropen']);
                $str = "<p class='myradio mrclose'><label class='open' style='display: none;'><input type='radio' name='radio_{$this->id}' value='open' /></label><label class='close'><input type='radio' name='radio_{$this->id}' value='close' /></label></p>";
                return $str;
            },
            'listorder_input' => function () {
                return Html::textInput("listorders[{$this->id}]", $this->listorder, ['size' => 2, 'style'=>'margin-bottom:0;width:60px']);
            }
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => SynchronousAccessBehavior::className(),
            ],
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'menu',
                'fieldName' => 'module'
            ],
        ]);
    }
}
